package hhh.com.choose_class;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class find extends AppCompatActivity {
    private SharedPreferences sp;
    private TextView tv_name,tv_time,tv_teacher;
    private List<Map<String,String>> list;
    private MyAdapter adapter;
    private SharedPreferences.Editor editor;
    private ListView lv_user;
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        sp = getSharedPreferences("user",MODE_PRIVATE);
        editor = sp.edit();

        lv_user = (ListView) findViewById(R.id.lv_user);
        list = new ArrayList<>();
        getData();
        adapter = new MyAdapter(
                find.this,
                list,
                R.layout.item,
                new String[]{"name","time","teacher"},
                new int[]{R.id.tv_name,R.id.tv_time,R.id.tv_teacher}
        );

        lv_user.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
        adapter = new MyAdapter(
                find.this,
                list,
                R.layout.item,
                new String[]{"name","time","teacher"},
                new int[]{R.id.tv_name,R.id.tv_time,R.id.tv_teacher}
        );

        lv_user.setAdapter(adapter);
    }

    private class MyAdapter extends SimpleAdapter {
        public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource,
                         String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            tv_name = v.findViewById(R.id.tv_name);
            tv_time = v.findViewById(R.id.tv_time);
            tv_teacher = v.findViewById(R.id.tv_teacher);
            tv_name.setText(list.get(position).get("name"));
            tv_time.setText(list.get(position).get("time"));
            tv_teacher.setText(list.get(position).get("teacher"));
            return v;
        }
    }

    public List<Map<String,String>> getData(){
        list.clear();
        MySqlite mySQLite = new MySqlite(this, 1);
        SQLiteDatabase database = mySQLite.getReadableDatabase();
        Cursor cursor = database.rawQuery("select * from huo", null);
        System.out.println(cursor.getCount());
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String time = cursor.getString(cursor.getColumnIndex("time"));
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String teacher = cursor.getString(cursor.getColumnIndex("teacher"));

            Map<String,String> map = new HashMap<>();
            map.put("name",name);
            map.put("time",time);
            map.put("id",id);
            map.put("teacher",teacher);
            list.add(map);
        }
        return list;
    }

}
